﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

public partial class ArticleSEOList : AdminPage
{
    /// <summary>
    /// 用户ID
    /// </summary>
    protected int UserID
    {
        get { return ViewState["UserID"] == null ? 0 : Convert.ToInt32(ViewState["UserID"]); }
        set { ViewState["UserID"] = value; }
    }

    /// <summary>
    /// 文章网址
    /// </summary>
    protected string Url
    {
        get { return ViewState["Url"] == null ? "" : ViewState["Url"].ToString(); }
        set { ViewState["Url"] = value; }
    }

    /// <summary>
    /// 文章类型
    /// </summary>
    protected int UrlType
    {
        get { return ViewState["UrlType"] == null ? 0 : Convert.ToInt32(ViewState["UrlType"]); }
        set { ViewState["UrlType"] = value; }
    }

    /// <summary>
    /// 分类ID
    /// </summary>
    protected int SortID
    {
        get { return ViewState["SortID"] == null ? 0 : Convert.ToInt32(ViewState["SortID"]); }
        set { ViewState["SortID"] = value; }
    }

    /// <summary>
    /// 开始时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 结束时间
    /// </summary>
    protected string EndDateTime
    {
        get { return ViewState["EndDateTime"] == null ? "" : ViewState["EndDateTime"].ToString(); }
        set { ViewState["EndDateTime"] = value; }
    }

    /// <summary>
    /// 关键字
    /// </summary>
    protected string Keywords
    {
        get { return ViewState["Keywords"] == null ? "" : ViewState["Keywords"].ToString(); }
        set { ViewState["Keywords"] = value; }
    }

    /// <summary>
    /// 收录状态
    /// </summary>
    protected int Status
    {
        get { return ViewState["Status"] == null ? -1 : Convert.ToInt32(ViewState["Status"]); }
        set { ViewState["Status"] = value; }
    }

    protected int nNum = 0;

    public DateTime dDateTime = DateTime.Now;
    /// <summary>
    /// 文章收录排名情况
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitSort();
            BindUser();
            if (nAdminFeatures != 1)
            {
                UserID = nAdminUserID;
            }
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.ArticleSEO;
    }

    /// <summary>
    /// 分类下拉框
    /// </summary>
    protected void InitSort()
    {
        DataTable dtSort = GetAllSort();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("请选择分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();
        txtBeginDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        txtEndDateTime.Text= DateTime.Now.ToString("yyyy-MM-dd");
        BeginDateTime = DateTime.Now.ToString("yyyy-MM-dd");
        EndDateTime = DateTime.Now.ToString("yyyy-MM-dd");
        if (nAdminFeatures == 1)
        {
            UserListPanel.Visible = true;
            ListUserTitle.Visible = true;
        }
        else
        {
            UserListPanel.Visible = false;
            ListUserTitle.Visible = false;
        }
    }

    /// <summary>
    /// 绑定用户列表
    /// </summary>
    protected void BindUser()
    {
        DataTable dtUser = new BisAdminInfo().GetAllAdminList();
        ddlUser.Items.Clear();
        ddlUser.Items.Add(new ListItem("所有用户", "0"));
        foreach (DataRow drUser in dtUser.Rows)
        {
            ddlUser.Items.Add(new ListItem(drUser["Name"].ToString(), drUser["ID"].ToString()));
        }
        dtUser.Dispose();
    }

    /// <summary>
    /// 页面初始化
    /// </summary>
    protected void InitData()
    {
        DateTime dBeginTime = Convert.ToDateTime("1753-01-01 12:00:00");
        DateTime dEndTime = Convert.ToDateTime("9999-12-31 11:59:59");
        if (!string.IsNullOrEmpty(BeginDateTime))
        {
            dBeginTime = Convert.ToDateTime(BeginDateTime);
           
        }
        if (!string.IsNullOrEmpty(EndDateTime))
        {
            dEndTime = Convert.ToDateTime(EndDateTime);
            dEndTime = dEndTime.AddDays(1);
        }
        int nRecordCount = 0;
        int nPageCount = 0;
        DataTable dtArticleSEO = new BisArticleSEO().GetList(UserID, SortID, Url,Keywords, UrlType, dBeginTime, dEndTime,Status, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        nNum = (anpPage.CurrentPageIndex - 1) * anpPage.PageSize;
        nNum++;
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptArticle.DataSource = dtArticleSEO;
        rptArticle.DataBind();
        dtArticleSEO.Dispose();

        anpPage.CustomInfoHTML = string.Format("<div class=\"pagetag\">当前第<b>{0}</b>/<b>{1}</b>页 共<b>{2}</b>条记录</div>", new object[] { anpPage.CurrentPageIndex, anpPage.PageCount, anpPage.RecordCount });
    }

    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptArticle_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            //删除信息
            bool result = new BisArticleSEO().Delete(Convert.ToInt32(e.CommandArgument),nAdminUserID);
            Until.Alert(result ? "删除成功！" : "删除失败！");
            InitData();
        }
    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptArticle_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dt = (DataTable)((Repeater)sender).DataSource;
        ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
        if (nAdminFeatures == 1)
        {
            btnDelete.Visible = true;
        }
        else
        {
            if (Convert.ToDateTime(dt.Rows[e.Item.ItemIndex]["CreateTime"]) >Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")))
            {
                btnDelete.Visible = true;
            }
        }
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        Url = txtUrl.Text.Trim();
        if (!string.IsNullOrEmpty(Url))
        {
            if (Url.Substring(Url.Length - 1, 1) == "/")
            {
                Url = Url.Substring(0, Url.Length - 1);
            }
        }
        Keywords = txtKeywords.Text.Trim();
        if (nAdminFeatures == 1)
        {
            UserID = Convert.ToInt32(ddlUser.SelectedValue);
        }
        else
        {
            UserID = nAdminUserID;
        }
        UrlType = Convert.ToInt32(ddlUrlType.SelectedValue);
        SortID = Convert.ToInt32(ddlSort.SelectedValue);
        BeginDateTime = txtBeginDateTime.Text.Trim();
        EndDateTime = txtEndDateTime.Text.Trim();
        Status = Convert.ToInt32(ddlStatus.SelectedValue);
        anpPage.CurrentPageIndex = 1;
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 验证是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckShow()
    {
        if (nAdminFeatures == 1)
        {
            return "";
        }
        else
        {
            return " style=\"display:none;\"";
        }
    }

    /// <summary>
    /// 详情显示控制
    /// </summary>
    /// <param name="oStatus"></param>
    /// <param name="oUrlType"></param>
    /// <returns></returns>
    protected string CheckInfoShow(object oStatus, object oUrlType)
    {
        if (Convert.ToBoolean(oStatus))
        {
            if(Convert.ToInt32(oUrlType)==3)
                return " style=\"display:none;\"";
            return "";
        }
        return " style=\"display:none;\"";
    }

    /// <summary>
    /// 获取类型名称
    /// </summary>
    /// <param name="nUrlType"></param>
    /// <returns></returns>
    protected string GetTypeName(int nUrlType)
    {
        switch (nUrlType)
        {
            case 1:
                return "新闻源";
            case 2:
                return "外链";
            case 3:
                return "站内";
        }
        return "";
    }
}